//
//  MyMateListViewController.m
//  TQY
//
//  Created by 甘世清 on 2024/9/29.
//

#import "MyMateListViewController.h"
#import "IssueMateViewController.h"
#import "MyMateTableViewCell.h"
#import "MateModel.h"
@interface MyMateListViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic , strong) HeaderView  * addBtn;
@end

@implementation MyMateListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.zx_hideBaseNavBar = YES;
}

- (void)SQ_viewWillAppear:(BOOL)animated{
    if (self.jsonArray.count<=0) {
        [self SQ_Header];
    }
}
- (void)SQ_UIViewForSelfView{
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
         make.edges.equalTo(self.view);
    }];
    [self.view addSubview:self.addBtn];
    [self.addBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.view);
            make.width.mas_equalTo(SCREEN_WIDETH-50);
            make.height.mas_equalTo(44);
        make.bottom.equalTo(self.view.mas_bottom).offset(-FootHeight-20);
    }];
    [self SQ_tableViewAddRefresh];
    [self SQ_Header];

}
- (void)SQ_Header{
    NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
    [dic setObject:@"1" forKey:@"current"];
    [dic setObject:@"20" forKey:@"size"];
    [dic setObject:@"0" forKey:@"type"];
    [HttpTool post:@"lug/myList" paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
        self.page = 1;
        [self.tableView.mj_header endRefreshing];
        NSDictionary * data = [[NSDictionary alloc]initWithDictionary:httpModel.data];
        NSArray * array = [MateModel mj_objectArrayWithKeyValuesArray:[data objectForKey:@"records"]];
        [self.jsonArray removeAllObjects];
        [self.jsonArray addObjectsFromArray:array];
        [self.tableView reloadData];
        if (self.jsonArray.count == 0) {
            self.addBtn.hidden = NO;
        } else {
            self.addBtn.hidden = YES;
        }
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        [self.tableView.mj_header endRefreshing];
        [YJProgressHUD showMsgWithoutView:httpModel.message];
    }];
}
- (void)SQ_Foother{
    NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
    [dic setObject:[NSString stringWithFormat:@"%d",self.page+1] forKey:@"current"];
    [dic setObject:@"20" forKey:@"size"];
    [dic setObject:@"0" forKey:@"type"];
    [HttpTool post:@"lug/myList" paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
        self.page ++;
        NSDictionary * data = [[NSDictionary alloc]initWithDictionary:httpModel.data];
        NSArray * array = [MateModel mj_objectArrayWithKeyValuesArray:[data objectForKey:@"records"]];
        [self.jsonArray addObjectsFromArray:array];
        [self.tableView reloadData];
        [self.tableView.mj_footer endRefreshing];
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        [self.tableView.mj_footer endRefreshing];
    }];
}

-(void)SQ_addButtonClick{
    IssueMateViewController * vc = [[IssueMateViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)SQ_deletedMate:(MateModel*)model{
    WS(weakSelf);
    [YJProgressHUD SQ_showMsgWithoutView:@""];
    NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
    [dic setObject:model.userid forKey:@"id"];
    [HttpTool post:@"lug/delete" paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
        [weakSelf.jsonArray removeObject:model];
        [weakSelf.tableView reloadData];
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        [YJProgressHUD showMsgWithoutView:httpModel.message];
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
   return self.jsonArray.count ;
}

 
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MyMateTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyMateTableViewCell"];
    if (!cell) {
        cell = [[MyMateTableViewCell alloc] initWithStyle:(UITableViewCellStyleValue1) reuseIdentifier:@"MyMateTableViewCell"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = [self.jsonArray  objectAtIndex:indexPath.item];
    WS(weakSelf);
    [cell setTouchBlock:^(MateModel * _Nonnull model) {
        [weakSelf SQ_deletedMate:model];
    }];
    return cell;
}
 

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return [MyMateTableViewCell FSCellHeightForTableView:tableView indexPath:indexPath cellContentViewWidth:0 bottomOffset:10];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
     
    
}

- (HeaderView *)addBtn{
    if (!_addBtn) {
        _addBtn = [[HeaderView alloc]init];
        _addBtn.backgroundColor = SQColor(@"#3DA59D");
        _addBtn.text = SQNSLocalString(@"去发布");
        _addBtn.colorString = @"#FFFFFF";
        _addBtn.hidden = YES;
        _addBtn.font = FONT_MEDIUM(14);
        _addBtn.layer.cornerRadius = 22;
        [_addBtn.btn addTarget:self action:@selector(SQ_addButtonClick) forControlEvents:UIControlEventTouchUpInside];
        _addBtn.layer.masksToBounds = YES;
    }
    return _addBtn;
}
@end
